<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:include value="/comm/header.jsp" />
<s:include value="/comm/menu.jsp" />
<!--第1部分开始  -->
<div class="wrap1">
	<div class="blank1"></div>
	<div class="outbox">
		<!--选项卡开始-->
		<s:form id="form1" name="addCanBuy" action="addCanBuy" method="post"
			enctype="multipart/form-data">
			<input type="hidden" name="canBuyModel.user.userid"
				value="<s:property value='#session.user.userid' />" />
			<input type="hidden" name="canBuyModel.isOpen" value="0" />
			<div id="mainTabbox7">
				<div class="tab7_tt">发布代购信息</div>
				<div id="tabbox7" class="tabbox7_box">
					<ul>
						<li id="taba1" class="hover"><a>基本信息</a></li>
						<li id="taba2"><a>物品详情</a></li>
						<li id="taba3"><a>运输与服务</a></li>
					</ul>
				</div>
				<div id="conten">
					<div class="tab_wrap7">
						<div>其中加*的为必填项,其他为选填项</div>
						<div class="tab7_show" id="con_taba_1">
							<div class="input_box_addCanBuy">
								<!--选项卡1内容开始-->
								<div class="wrap_left" style="width:50%">
									<!-- -->
									<div class="input_box">
										<ul>
											<li class="t1"><span class="span33">名称</span></li>
											<li class="t2"><input id="goodsName"
												name="canBuyModel.goodsName" type="text" class="input1" maxlength="35"/><label style="color:red">&nbsp;*</label><br /></li><div class="prompt_ie6">
												<label id="goodsNamePrompt"
												style="display:none; color: red;margin-left:70px;line-height:15px;">物品名称不能为空</label><br/></div>
											<li class="t1"><span class="span33">类别</span></li>
											<li class="t2"><s:select name="canBuyModel.categoryId"
													list="categorys" listKey="categoryId"
													listValue="categoryName" /></li>
											<li class="t1"><span class="span33">所属区域</span></li>
											<li class="t2"><s:select name="canBuyModel.areaId"
													list="areas" listKey="areaId" listValue="areaName" /></li>
											<li class="t1"><span class="span33">价格</span></li>
											<li class="t2"><input id="price"
												name="canBuyModel.price" type="text" class="input1" />
<label style="color:red">&nbsp;*</label><br /></li>
<div class="prompt_ie6">
												<label id="pricePrompt" style="display:none;margin-left:70px;line-height:15px;color: red">请输入正确的价格</label><br/></div>											
											<li class="t1"><span class="span33">佣金比例</span></li>
											<li class="t2"><input id="ratio" name="canBuyModel.ratio" type="text"
												class="input1" /><label style="color:red">&nbsp;*</label>%<br/></li>
<div class="prompt_ie6"> <label id="ratioPrompt" style="display:none; color: red;margin-left:70px;line-height:15px;"></label><br/></div>
											<li class="t1"><span class="span33">佣金</span></li>
											<li class="t2"><input id="serviceFee"
												name="canBuyModel.serviceFee" type="text" class="input1"
												readonly="readonly" /></li>
											<li class="t1"><span class="span33">国内价</span></li>
											<li class="t2">
												<input type="hidden" name="canBuyModel.domesticPrice" id="domesticPriceId" value="0" />
												<input id="domesticPrice" name="domesticPrice" type="text" class="input1" /><label style="color:red"></label><br/>
												<label id="domesticPricePrompt" style="color: red"></label>
											</li>
											<li class="t1"><span class="span33">来源</span></li>
											<li class="t2"><s:select name="canBuyModel.source"
													list='#{"1":"大型超市","2":"便利店","3":"专卖店","4":"生产厂家","5":"其他" }'
													theme="simple" /></li>
											<li class="t1"><span class="span33">单据类型</span></li>
											<li class="t2"><s:select name="canBuyModel.receipts"
													list='#{"1":"小票","2":"发票","3":"发票或小票复印件" }' theme="simple" /></li>

										</ul>

									</div>
									<div class="clear"></div>
									<!-- -->
								</div>
								<div class="wrap_left">
									<!-- -->
									<div class="input_box">
										<ul>
											<li class="t1"><span class="span33">首件运费</span></li>
											<li class="t2">
												<input id="shippingCost" name="canBuyModel.logistics.price" type="text" class="input1" value=""/>
												<br/>
												<label id="shippingCostPrompt" style="color: red"></label>
											</li>
											<li class="t1"><span class="span33">续件运费</span></li>
											<li class="t2">
												<input id="continuedPrice" name="canBuyModel.logistics.continuedPrice" type="text" class="input1" value="" />
												<br/>
												<label id="continuedPricePrompt" style="color: red"></label>
											</li>
											<li class="t1"><span class="span33">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;描述</span></li>
											<li class="t2"><input name="canBuyModel.description"
												type="text" class="input1" maxlength="23"/></li>
											<li class="t1"><span class="span33">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;图片</span></li>
											<li class="t2">
												<label id="imagePrompt" style="line-height:30px;color:red;"></label><br/>
												<ul>
													<li><s:file name="image" cssClass="input5"></s:file></li>
													<li><s:file name="image" cssClass="input5"></s:file></li>
													<li><s:file name="image" cssClass="input5"></s:file></li>
													<li><s:file name="image" cssClass="input5"></s:file></li>
													<li><s:file name="image" cssClass="input5"></s:file></li>
												</ul>
												
											</li>
											
										</ul>

									</div>
									<div class="clear"></div>
									<!-- -->
								</div>

								<div class="clear"></div>
								


								<!--选项卡1内容结束-->
							</div>

						</div>
						<div class="tab7_show" id="con_taba_2">
							<textarea name="canBuyModel.details" cols="160" rows="10" class="xheditor"></textarea>
						</div>
						<div class="tab7_show" id="con_taba_3">
							<b>请填写配送信息、售后服务条款以及关税等注意事项</b><br/>
							<s:textarea name="canBuyModel.serviceAssurance" cols="160"
								rows="10"  cssClass="xheditor"></s:textarea>
						</div>

					</div>
				</div>
			</div>
		</s:form>
		<!--选项卡结束-->
		<div class="but_submit" style="text-align: center">
			<br />
			<a id="addCanBuy" href="javascript:addCanBuy();">确认无误，提交</a>	
		</div>
	</div>

</div>
<s:include value="/comm/bottom.jsp" />
<script type="text/javascript">

var submit = false;
$(window).load(function(){
	$("#goodsName").blur(validateGoodsName);
	$("#price").blur(function(){if (validatePrice()) {calcServiceFee();}});
	$("#ratio").blur(function() {if (validateRatio()) {calcServiceFee();}});
	$("#domesticPrice").blur(validateDomesticPrice);
	$("#shippingCost").blur(function(){validateShippingCost($(this).val(),"shippingCostPrompt");});
	$("#continuedPrice").blur(function(){validateShippingCost($(this).val(),  "continuedPricePrompt");});
	
	$("#taba1").click(function(){setTab('taba',1,3);});
	$("#taba2").click(function(){setTab('taba',2,3);});
	$("#taba3").click(function(){setTab('taba',3,3);});
	
	$("[name=image]").each(function() {
		$(this).change(function() {
			var image = $(this).val();
			if(!/\.(jpeg|png|JPEG|PNG)$/.test(image)) {
				$("#imagePrompt").html("图片类型必须是.jpeg,png中的一种");
	        }
			else {
				$("#imagePrompt").html("");	
			}
		});
	});
});



function addCanBuy() {
	if (!validateGoodsName()) {
		return;
	}
	if (!validatePrice()) {
		return;
	}
	if (!validateRatio()) {
		return;
	}
	if (!validateDomesticPrice()) {
		return;
	}
	if (!validateShippingCost()) {
		return;
	}
	
	if (!validateImage()) {
		return;
	}
	
	if (trim($("#shippingCost").val()) == "") {
		$("#shippingCost").val(0);
	}
		
	
	
	var domesticPrice = $("#domesticPrice").val();
	if (domesticPrice != "" && domesticPrice != "国内无此产品") {
		$("#domesticPriceId").val($("#domesticPrice").val());
	}
	
	if (!submit) {
		submit = true;
		$("#form1").submit();	
	}
	
		
}

function validateImage() {
	var images = $("[name=image]");
	var empty = true;
	var valid = true;
	for (var i = 0; i < images.length; i++) {
		if (images[i].value != "") {
			empty = false;
			if (!/\.(jpeg|png|JPEG|PNG)$/.test(images[i].value)) {
				valid = false;
				break;
			}
		}
	}
	if (empty) {
		$("#imagePrompt").html("请至少选择一张图片");
		return false;
	}
	if (!valid) {
		$("#imagePrompt").html("图片类型必须是.jpeg,png中的一种");
		return false;
	}
	return true;
}

function validateGoodsName() {
	var goodsName = trim($("#goodsName").val());
	if (goodsName == "") {
		$("#goodsNamePrompt").html("物品名称不能为空");
		$("#goodsNamePrompt").show();
		return false;
	}
	else {
		$("#goodsNamePrompt").hide();
		return true;
	}
}
	
function validatePrice() {
	var price = trim($("#price").val());
	if (price == "") {
		$("#pricePrompt").html("价格不能为空");
		$("#pricePrompt").show();
		return false;
	}
	else if (!priceRegex.test(price) || price <= 0) {
		$("#pricePrompt").html("请输入正确的价格");
		$("#pricePrompt").show();
		return false;
	} 
	else {
		$("#pricePrompt").hide();
		return true;
	}
}
	
function validateRatio() {
	var ratio = trim($("#ratio").val());
	if (ratio == "") {
		$("#ratioPrompt").html("佣金比例不能为空");
		$("#ratioPrompt").show();
		return false;
	}
	if (!digitRegex.test(ratio) || ratio <= 0) {
		$("#ratioPrompt").html("只能输入正整数");
		$("#ratioPrompt").show();
		return false;
	}
	$("#ratioPrompt").hide();
	return true;
}

//计算佣金
function calcServiceFee() {
	var price = $("#price").val();
	var ratio = $("#ratio").val();
	if (price != "" && priceRegex.test(price) && price > 0 && ratio != "" && digitRegex.test(ratio) && ratio > 0) {
		var serviceFee = price * ratio / 100;
		$("#serviceFee").val(serviceFee.toFixed(2));	
	}
}

function validateDomesticPrice() {
	var price = trim($("#domesticPrice").val());
	if (price == "" || price == "国内无此产品") {
		return true;
	}
	if (!priceRegex.test(price) || price <= 0) {
		$("#domesticPricePrompt").html("请输入正确的价格");
		$("#domesticPricePrompt").show();
		return false;
	}
	$("#domesticPricePrompt").hide();
	return true;
	
}

function validateShippingCost(price, pricePromptId) {
	if (arguments.length == 0) {
		return (validateShippingCost($("#shippingCost").val(), "shippingCostPrompt") && validateShippingCost($("#continuedPrice").val(), "continuedPricePrompt")); 
	}
	else {
		if (price != "" && (!priceRegex.test(price) || price < 0)) {
			$("#" + pricePromptId).html("请输入正确的运费");
			return false;
		}
		$("#" + pricePromptId).html("");
		return true;
	}	
}


	
</script>